package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.BasePojo;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Xiao
 * @date 2021/10/22
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo selectById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public List<UserInfo> selectByIds(List<Long> userIds) {
        return userInfoMapper.selectBatchIds(userIds);
    }

    @Override
    public Map<Long, UserInfo> selectByIdsAndCondition(List<Long> userIds, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId, userIds);
        if (userInfo != null) {
            if (userInfo.getAge() != null) {
                wrapper.le(UserInfo::getAge, userInfo.getAge());
            }
            if (StringUtils.isNotBlank(userInfo.getGender())) {
                wrapper.eq(UserInfo::getGender, userInfo.getGender());
            }
            if (StringUtils.isNotBlank(userInfo.getNickname())) {
                wrapper.like(UserInfo::getNickname, userInfo.getNickname());
            }
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        //使用CollUtil.fieldValueMap()构造Map对象
//        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfos, "id");
        //使用stream().collect()构造Map对象
        return userInfos.stream().collect(Collectors.toMap(UserInfo::getId, Function.identity()));
    }

    @Override
    public List<UserInfo> list() {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        return userInfoMapper.selectList(wrapper);
    }

    @Override
    public PageResult selectPageUserInfos(Integer page, Integer pagesize) {
        IPage<UserInfo> iPage = new Page<>(page, pagesize);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UserInfo::getCreated);
        userInfoMapper.selectPage(iPage, wrapper);
        List<UserInfo> userInfos = iPage.getRecords();
        Integer counts = Integer.parseInt(String.valueOf(iPage.getTotal()));
        if (CollUtil.isEmpty(userInfos)) {
            return new PageResult();
        }
        return new PageResult(page, pagesize, counts, userInfos);
    }
}
